package com.example.seckill.Exception;

import com.example.seckill.common.CommonResult;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public CommonResult ExceptionHandler(Exception e) {
        if (e instanceof GlobalException) {
            GlobalException exception = (GlobalException) e;
            return CommonResult.error(exception.getErrorEnumEnum());
        } else if (e instanceof BindException) {
            BindException bindException = (BindException) e;
            CommonResult respBean = CommonResult.error(ErrorEnum.BIND_ERROR);
            respBean.setMsg("参数校验异常：" + bindException.getBindingResult().getAllErrors().get(0).getDefaultMessage());
            return respBean;
        }
        System.out.println("异常信息" + e);
        return CommonResult.error(ErrorEnum.ERROR);
    }
}